package com.woniuxy.oa.web;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.oa.entity.Employee;
import com.woniuxy.oa.entity.ResponseVO;
import com.woniuxy.oa.entity.query.EmployeeQuery;
import com.woniuxy.oa.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * The type Employee controller.
 * The type Employee controller.员工管理
 *
 * @author 毓
 */
@RequestMapping("/employee")
@RestController
@CrossOrigin("*")
public class EmployeeController {
    /**
     * The Employee service.
     */
    @Autowired
    @Qualifier("employeeService")
    private EmployeeService employeeService;

    /**
     * List response vo.
     *
     * @param employeeQuery 查询条件
     * @return the string 员工信息+部门信息
     */
    @PostMapping("/list")
    @ResponseBody
    public ResponseVO<PageInfo> list(@RequestBody EmployeeQuery employeeQuery) {
        //分页设置,起始页PageNum,分页大小为3
        PageHelper.startPage(employeeQuery.getPageNum() == null ? 1 : employeeQuery.getPageNum(), 3);
        List<Employee> employees = employeeService.query(employeeQuery);
        ResponseVO<PageInfo> vo = new ResponseVO<>();
        PageInfo<Employee> pageInfo = new PageInfo<>(employees);
        vo.setData(pageInfo);
        vo.setCode("200");
        return vo;
    }

    /**
     * Add string.
     * 员工增加
     *
     * @param employee the employee
     * @return the string
     */
    @PostMapping("/add")
    @ResponseBody
    public ResponseVO add(@RequestBody Employee employee) {
        employeeService.add(employee);
        ResponseVO<Employee> vo = new ResponseVO<>();
        vo.setCode("200");
        return vo;
    }

    /**
     * Update response vo.
     * 员工资料修改
     *
     * @param employee the employee
     * @return the response vo
     */
    @PostMapping("/update")
    @ResponseBody
    public ResponseVO<Employee> update(@RequestBody Employee employee) {
        ResponseVO<Employee> vo = new ResponseVO<>();
        employeeService.update(employee);
        Employee e = employeeService.getById(employee.getId());
        vo.setCode("200");
        vo.setData(e);
        vo.setMsg("修改成功");
        return vo;
    }


    /**
     * Lock response vo.
     * 锁定员工
     *
     * @param id the id
     * @return the response vo
     */
    @PostMapping("/lock")
    @ResponseBody
    public ResponseVO<Employee> lock(@RequestBody String id) {
        ResponseVO<Employee> vo = new ResponseVO<>();
        employeeService.lock(id);
        vo.setCode("200");
        vo.setMsg("删除成功");
        return vo;
    }

    /**
     * Use response vo.
     * 启用员工账号
     *
     * @param id the id
     * @return the response vo
     */
    @CrossOrigin("*")
    @PostMapping("/use")
    @ResponseBody
    public ResponseVO<Employee> use(@RequestBody String id) {
        ResponseVO<Employee> vo = new ResponseVO<>();
        employeeService.use(id);
        vo.setCode("200");
        vo.setMsg("删除成功");
        return vo;
    }

    /**
     * View response vo.
     * 查看员工信息
     *
     * @param id the id
     * @return the response vo
     */
    @PostMapping("/view")
    @ResponseBody
    public ResponseVO<Employee> view(@RequestBody String id) {
        ResponseVO<Employee> vo = new ResponseVO<>();
        Employee employee = employeeService.getById(id);
        vo.setCode("200");
        vo.setData(employee);
        return vo;
    }

}

